@model cloudscribe.Core.Web.ViewModels.IpAddresses.PaginatedIpAddressesViewModel
@inject IStringLocalizer<CloudscribeCore> sr
@inject ICoreThemeHelper themeHelper
@{
    var themeSettings = themeHelper.GetThemeSettings();
    if (themeSettings.AdminSideNavExpanded) { ViewData["SideNavToggle"] = "show"; }
    ViewData["SideNavVisible"] = true;
}

<h2>@ViewBag.Title</h2>

@if (ViewBag.IpRestrictionsEnabled == false)
{
    <div class="alert alert-warning" role="alert">
        <strong>⚠️ @sr["Warning:"]</strong> @sr["IP address restrictions are currently disabled in the application configuration. Any IP addresses you configure here will be stored but will not be enforced until the feature is enabled in appsettings.json (SiteConfigOptions:EnableIpAddressRestrictions)."]
    </div>
}

<div class="row">
    <div class="col mb-3">
        <h4>@sr["Your IP Address:"] @ViewBag.UsersIpAddress</h4>
        <p>@sr["Permitting one or more IP addresses here will automatically block access from all other IP addresses, unless specific blocked IP addresses or ranges have been defined. If an IP address is both blocked and permitted, the permit will take precedence."]</p>
    </div>
</div>
@{
    if (@ViewBag.status != null)
    {
        <div class="alert alert-info">@ViewBag.status</div>
    }
}
<div class="row align-items-center">
    @{
        if (Model.BlockedPermittedIpAddresses.Data.Count == 0)
        {
            <div class="alert alert-info">@sr["No IP addresses have been permitted."]</div>
            <div class="col d-flex">
                <button name="AddNewRow" class="btn btn-secondary mt-2 me-3" data-bs-toggle="modal" data-bs-target="#AddNewIpAddress">@sr["Add New IP Address"]</button>
                <button name="BulkUpload" class="btn btn-secondary mt-2" data-bs-toggle="modal" data-bs-target="#BulkUploadIpAddresses">@sr["Bulk Upload IP Addresses"]</button>
            </div>
        }
        else
        {
            <div class="col-sm-12 col-md-6 mb-3 d-flex">
                <form method="post" class="form-horizontal" role="form" asp-antiforgery="true" data-submit-once="true" asp-controller="SiteAdmin" asp-action="SearchPermittedIpAddresses">
                    <div class="input-group">
                        <input asp-for="@Model.SearchTerm" type="text" id="searchTerm" name="searchTerm" />
                        <button class="btn btn-outline-secondary" type="button" id="RemoveSearch" disabled="true" onclick="location.href='@Url.Action("PermittedIpAddresses", "SiteAdmin")'">X</button>
                        <button id="searchButton" class="btn btn-primary ms-3" type="submit" name="submit" aria-expanded="false" disabled="true">@sr["Search"]</button>
                    </div>
                </form>
            </div>
            <div class="col-sm-12 col-md-6 mb-3 d-flex justify-content-end">
                <button name="AddNewRow" class="btn btn-secondary mt-2 me-3" data-bs-toggle="modal" data-bs-target="#AddNewIpAddress">@sr["Add New IP Address"]</button>
                <button name="BulkUpload" class="btn btn-secondary mt-2" data-bs-toggle="modal" data-bs-target="#BulkUploadIpAddresses">@sr["Bulk Upload IP Addresses"]</button>
            </div>
        }
    }
</div>

<div asp-validation-summary="All" class="text-danger"></div>

@for (var i = 0; i < Model.BlockedPermittedIpAddresses.Data.Count; i++)
{
    var safeId = Model.BlockedPermittedIpAddresses.Data[i].Id.ToString().Replace("-", "_");
    <form method="post" class="form-horizontal" role="form" asp-antiforgery="true" data-submit-once="true" asp-controller="SiteAdmin" asp-action="UpdatePermittedIpAddress">
        <input asp-for="@Model.BlockedPermittedIpAddresses.Data[i].Id" name="Id" id="Id" type="hidden" />
        <input asp-for="@Model.BlockedPermittedIpAddresses.Data[i].SiteId" name="SiteId" id="SiteId" type="hidden" />
        <input asp-for="@Model.BlockedPermittedIpAddresses.Data[i].CreatedDate" name="CreatedDate" id="CreatedDate" type="hidden" />
        <input asp-for="@Model.BlockedPermittedIpAddresses.Data[i].LastUpdated" name="LastUpdated" id="LastUpdated" type="hidden" />
        <div class="row bg-light">
            <div class="col-sm-12 col-md-2 mb-3 d-flex flex-wrap align-items-center">
                @if (i == 0)
                {
                    <label class="form-label" style="display: block;width: 100%;margin-bottom: 0.15rem;">&nbsp;</label>
                }
                <div id="@("EditButton" + @safeId)">
                    <button name="edit" type="button" class="btn btn-primary m-1" data-disabled-text='@sr["Working..."]' data-enabled-text='@sr["Edit"]' onclick="EditItem('@safeId')">@sr["Edit"]</button>
                </div>
                <div id="@("EditingButtons" + @safeId)" style="display: none;">
                    <button name="submit" type="submit" class="btn btn-primary m-1" data-disabled-text='@sr["Working..."]' data-enabled-text='@sr["Save"]'>@sr["Save"]</button>
                    <button name="delete" id="delete" type="button" class="btn btn-danger m-1" data-disabled-text='@sr["Working..."]' data-enabled-text='@sr["Delete"]' data-bs-toggle="modal" data-bs-target="#DeleteConfirmation" data-id="@safeId">@sr["Delete"]</button>
                    <button name="cancel" type="button" class="btn btn-secondary m-1" data-disabled-text='@sr["Working..."]' data-enabled-text='@sr["Cancel"]' onclick="CancelEditing('@safeId')">@sr["Cancel"]</button>
                </div>
            </div>
            <div class="col-sm-12 col-md-2 mb-3 align-items-center">
                @if (i == 0)
                {
                    <label asp-for="@Model.BlockedPermittedIpAddresses.Data[i].IpAddress" name="IpAddress" id="IpAddress" class="form-label">@sr["Permitted IP"]</label>
                }
                <div class="mb-3" id="errorMessage-@safeId" style="display:none;"></div>
                <input asp-for="@Model.BlockedPermittedIpAddresses.Data[i].IpAddress" name="IpAddress" id="IpAddress-@safeId" disabled="true" class="form-control ip-address-input" data-safeid="@safeId" />
                <div class="form-check form-switch mt-1 mx-2 align-items-center" style="display:none;" id="@("ipRangeSwitch" + @safeId)">
                    <input asp-for="@Model.BlockedPermittedIpAddresses.Data[i].IsRange" class="form-check-input" type="checkbox" id="IsRange" name="IsRange">
                    <label class="form-check-label" for="IsRange">IP Range</label>
                </div>
            </div>
            <div class="col-sm-12 col-md-4 mb-3 align-items-center">
                @if (i == 0)
                {
                    <label asp-for="@Model.BlockedPermittedIpAddresses.Data[i].Reason" name="Reason" id="Reason" class="form-label">@sr["Reason"]</label>
                }
                <input asp-for="@Model.BlockedPermittedIpAddresses.Data[i].Reason" name="Reason" id="Reason" disabled="true" class="form-control" />
            </div>
            <div class="col-sm-12 col-md-2 mb-3 align-items-center">
                @if (i == 0)
                {
                    <label asp-for="@Model.BlockedPermittedIpAddresses.Data[i].CreatedDate" name="CreatedDate" id="CreatedDate" class="form-label">@sr["Created Date"]</label>
                }
                <label asp-for="@Model.BlockedPermittedIpAddresses.Data[i].CreatedDate" style="background-color: #e9ecef;opacity: 1;" class="form-control">@Model.BlockedPermittedIpAddresses.Data[i].CreatedDate.ToString(@"dd\/MM\/yyyy")</label>
            </div>
            <div class="col-sm-12 col-md-2 mb-3 align-items-center">
                @if (i == 0)
                {
                    <label asp-for="@Model.BlockedPermittedIpAddresses.Data[i].LastUpdated" name="LastUpdated" id="LastUpdated" class="form-label">@sr["Last Updated"]</label>
                }
                <label asp-for="@Model.BlockedPermittedIpAddresses.Data[i].LastUpdated" style="background-color: #e9ecef;opacity: 1;" class="form-control">@Model.BlockedPermittedIpAddresses.Data[i].LastUpdated.ToString(@"dd\/MM\/yyyy")</label>
            </div>
        </div>
    </form>
}

<cs-pager asp-action="Index" asp-controller="SiteAdmin"
          asp-route-pagesize="@Model.BlockedPermittedIpAddresses.PageSize"
          cs-paging-pagenumber="@Model.BlockedPermittedIpAddresses.PageNumber"
          cs-paging-totalitems="@Model.BlockedPermittedIpAddresses.TotalItems"
          asp-route-q="@Model.SearchTerm"
          cs-first-page-title='@sr["First Page"]'
          cs-last-page-title='@sr["Last Page"]'
          cs-previous-page-title='@sr["Previous page"]'
          cs-next-page-title='@sr["Next page"]'
          cs-pager-ul-class="pagination mt-3 d-flex flex-wrap"
          cs-pager-li-current-class="page-item active"
          cs-pager-li-other-class="page-item"
          cs-pager-li-non-active-class="page-item disabled"
          cs-pager-link-current-class="page-link"
          cs-pager-link-other-class="page-link"></cs-pager>

<div class="modal fade" id="AddNewIpAddress" tabindex="-1" role="dialog" aria-labelledby="AddNewIpAddress">
    <div class="modal-dialog modal-md" role="document">
        <div class="modal-content" style="padding:1rem;">
            <partial name="PermittedIpAddresses/AddPermittedIpAddressPartial" model="new cloudscribe.Core.Web.ViewModels.IpAddresses.IpAddressesViewModel()" />
        </div>
    </div>
</div>

<div class="modal fade" id="DeleteConfirmation" tabindex="-1" role="dialog" aria-labelledby="DeleteConfirmation">
    <div class="modal-dialog modal-md" role="document">
        <div class="modal-content" style="padding:1rem;">
            <partial name="PermittedIpAddresses/DeletePermittedIpAddressPartial" model="string.Empty" />
        </div>
    </div>
</div>

<div class="modal fade" id="BulkUploadIpAddresses" tabindex="-1" role="dialog" aria-labelledby="BulkUploadIpAddresses">
    <div class="modal-dialog modal-md" role="document">
        <div class="modal-content" style="padding:1rem;">
            <partial name="PermittedIpAddresses/BulkUploadIpAddressesPartial" model="new cloudscribe.Core.Web.ViewModels.IpAddresses.BulkUploadIpAddressesModel()" />
        </div>
    </div>
</div>

@section SideNav {
    <partial name="AdminSideNav" />
}
@section Toolbar {
    <partial name="AdminSideNavToggle" />
}
@section Scripts {
    <partial name="AdminSideNavScripts" />
    <script src="~/cr/js/jquery.validate.min.js"></script>
    <script src="~/cr/js/jquery.validate.unobtrusive.min.js"></script>
    <script src="~/cr/js/unobtrusive-validation-bs4.min.js"></script>
    <script src="~/cr/js/cloudscribe-cascade-unobtrusive.min.js"></script>
    <script src="~/cr/js/jquery.validate.hooks.min.js"></script>
    <script src="~/cr/js/jqueryvaildation.submitonce-unobtrusive.min.js"></script>
    <script src="~/cr/js/IpAddressStateAndValidation.min.js"></script>
}